package com.my.socket.nio.v8;

import java.io.IOException;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;

/**
 * Created by Scarlett on 2018/7/2 0002.
 */
public class SocketAcceptHandler extends SocketHandler {

    public SocketAcceptHandler(ServerDispatcher serverDispatcher, ServerSocketChannel serverSocketChannel, Selector selector) throws Exception {
        super(serverDispatcher, serverSocketChannel, selector);
        serverSocketChannel.register(this.selector, SelectionKey.OP_ACCEPT, this);
    }


    @Override
    public void runnerExecute(int readyKeyOps) throws IOException {
        if (readyKeyOps == SelectionKey.OP_ACCEPT) {
            socketChannel = serverSocketChannel.accept();
            socketChannel.configureBlocking(false);
            LOGGER.debug("Server accept");

            socketChannel.register(serverDispatcher.getReadSelector(), SelectionKey.OP_READ);
        }
    }
}
